package com.chen.excel;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ImageRecordRepository extends MongoRepository<ImageRecord, String> {
    
    Optional<ImageRecord> findByWaybillNo(String waybillNo);
    
    List<ImageRecord> findByDriverName(String driverName);
    
    List<ImageRecord> findByPlateNumber(String plateNumber);
    
    Optional<ImageRecord> findByWaybillNoAndDriverNameAndPlateNumber(
            String waybillNo, String driverName, String plateNumber);
    
    @Query("{ $or: [ { 'waybillNo': ?0 }, { 'driverName': ?1 }, { 'plateNumber': ?2 } ] }")
    List<ImageRecord> findByAnyField(String waybillNo, String driverName, String plateNumber);
    
    @Query("{ $and: [ " +
           "{ $or: [ { 'waybillNo': { $exists: false } }, { 'waybillNo': '' }, { 'waybillNo': ?0 } ] }, " +
           "{ $or: [ { 'driverName': { $exists: false } }, { 'driverName': '' }, { 'driverName': ?1 } ] }, " +
           "{ $or: [ { 'plateNumber': { $exists: false } }, { 'plateNumber': '' }, { 'plateNumber': ?2 } ] } ] }")
    List<ImageRecord> findByMatchingFields(String waybillNo, String driverName, String plateNumber);
}